package com.ro.model.file;

import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.ro.model.file.upload.returns.EditormdUploadRet;
import com.ro.model.file.upload.returns.FileDataRet;
import com.ro.model.file.upload.returns.FileRet;

import java.io.File;

public class FileUploadModel extends Controller {

    public void index() {
        renderJson("不开放哦...");
    }

    public void upload() {
        FileRet fileRet = null;
        try {
            UploadFile file = this.getFile();
            File delfile = new File(file.getUploadPath() + "\\" + file.getFileName());
            System.out.println("==========" + delfile.getPath());
            FileDataRet fileDataRet = new FileDataRet(delfile.getPath(), "123123");
            fileRet = new FileRet("0", "文件上传成功", fileDataRet);
        } catch (Exception e) {
            e.printStackTrace();
            fileRet = new FileRet("1", "文件上传失败");
        }
        renderJson(fileRet);
    }


    public void uploadEditor() {
        EditormdUploadRet editormdUploadRet = null;
        try {
            UploadFile file = this.getFile();
            String filePath = "/upload/" + file.getFileName();
            editormdUploadRet = new EditormdUploadRet((byte) 1, "文件上传成功", filePath);
        } catch (Exception e) {
            e.printStackTrace();
            editormdUploadRet = new EditormdUploadRet((byte) 0, "文件上传失败");
        }
        renderJson(editormdUploadRet);
    }

}
